﻿using System.Collections.Generic;
using System.Linq;
using Missbot.Common;
using Missbot.Core.Base;
using Dolany.UtilityTool;
using Missbot.Model.MsgInfos;

namespace Missbot.AIModule.Gaming.ChessAgainst
{
    public class ChessSvc : ISingleton
    {
        private readonly List<ChessEngine> WorkingEngine = new();

        public bool IsGroupInPlaying(string GroupID)
        {
            return WorkingEngine.Any(e => e.Group.ID == GroupID);
        }

        public bool IsUserInPlaying(string UserID)
        {
            return WorkingEngine.Any(e => e.AimUser.ID == UserID || e.SelfUser.ID == UserID);
        }

        public void StartAGame(InstanceGroup Group, InstanceUser FirstUser, InstanceUser SecondUser, IMsgChannel Channel)
        {
            var ran = Rander.RandInt(2);
            var SelfUser = ran == 0 ? FirstUser : SecondUser;
            var AimUser = ran == 1 ? FirstUser : SecondUser;

            var engine = AutofacSvc.Resolve<ChessEngine>();
            engine.Init(Group, SelfUser, AimUser, Channel);
            WorkingEngine.Add(engine);
            engine.GameStart();
        }

        public void GameOver(ChessEngine engine)
        {
            WorkingEngine.Remove(engine);
        }
    }
}